home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8886 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  48 lines

  1. Path: corona.laa.com.au!news
  2. From: Jason Collins <jason@www.inia.net.au>
  3. Newsgroups: comp.lang.c
  4. Subject: read/write integers to files
  5. Date: Thu, 07 Mar 1996 20:50:13 +1000
  6. Organization: Internet Image
  7. Message-ID: <313EBF65.4E82@www.inia.net.au>
  8. NNTP-Posting-Host: inia-a04.laa.com.au
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Hi there,
  15.  
  16. I would preface this question with the standard "dumb newbie question......." or whatever but I 
  17. think you'll work it out for yourselves soon enough.
  18.  
  19. My problem is that I'm trying to write a program that will read an integer from the file 
  20. count.dat, increment the integer then write it back to count.dat.  I have provided the listing so 
  21. that you can all tell me what I'm doing wrong.
  22.  
  23. Thanks....and.....be gentle.
  24.  
  25. --------------------
  26. counter.c
  27. --------------------
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. FILE *filePtr;
  33.  
  34. void main()
  35. {
  36.   char ctr;
  37.  
  38.   filePtr=fopen("count.dat", "ab");
  39.   fscanf(filePtr, "%d", &ctr);
  40.   fclose(filePtr);
  41.  
  42.   ctr++;
  43.  
  44.   filePtr=fopen("count.dat", "wb");
  45.   fprintf(filePtr, "%d", ctr);
  46.   fclose(filePtr);
  47. }
  48.